using Slaks.DocumentSerialization.Dictionary;
using Slaks.DocumentSerialization.Document;

namespace Slaks.Graphs.GraphBuilder.SchenkerDictionary
{
	public class SchenkerDictionaryBuilder:GraphBuilderBase
	{
		/// <summary>
		/// Add edges between nodes
		/// </summary>
		/// <param name="document"></param>
		/// <param name="graph">graph to build</param>
		/// <param name="dictionaryMap">DictionaryMap holds the dictionary by means of this dictionary the graph will be built</param>
		/// <param name="currentVertex">the current node</param>
		/// <param name="nParameter">the n-distance parameter</param>
		/// <param name="nDistanceCount">current distance counter</param>
		/// <param name="sentence">current sentence</param>
		/// <param name="sectionType">section type</param>
		protected void AddRelations(Document document,
		                            Graph graph,
		                            DictionaryMap dictionaryMap,
		                            Vertex currentVertex,
		                            int nParameter,
		                            int nDistanceCount,
		                            Sentence sentence,
		                            Section.SectionType sectionType,
		                            BuilderType builderType
			)
		{
			if (nDistanceCount > nParameter) return;
			Node node;
			long nextId;
			Vertex nextIdVertex;
			bool isIdInDictionary = false;
			if (sentence.NextId >= 0)
			{
				//get the nextId.
				nextId = sentence.NextId;
				//we must check whether the nextId is in the list of frequent nodes.
				//if not then we must skip it.

				isIdInDictionary = dictionaryMap.Contains(nextId);
				node = document.GetNodeById(nextId);
				Section section = node.GetSectionByType(sectionType);
				if (section == null) return;
				Sentence nextSentence = section.GetSentence(sentence.Number, sentence.Place + 1);
				if (nextSentence == null) return;

				string sectionLabel;

				if (isIdInDictionary != false) //the nextIdVertex is among frequent nodes
				{
					if (builderType == BuilderType.NDistanceDictionaryRepresentationSchenker) sectionLabel = nDistanceCount.ToString();
					else if (builderType == BuilderType.NSimpleDistanceDictionaryRepresentationSchenker) sectionLabel = "no";
					else if (builderType == BuilderType.NormalizedFrequencyDictionaryRepresentationSchenker ||
						builderType == BuilderType.RawFrequencyDictionaryRepresentationSchenker ||
						builderType == BuilderType.SimpleDictionaryRepresentationSchenker) sectionLabel = "no";
					else sectionLabel = section.GetSectionTypeString();

					nextIdVertex = graph.CreateAddVertex(nextId);
					currentVertex.AddEdge(nextIdVertex, sectionLabel);

					AddRelations(document, graph, dictionaryMap, currentVertex, nParameter, nDistanceCount + 1, nextSentence, sectionType, builderType);
				}
				else
				{
					//the nextIdVertex is not frequent, then we must skip it and find the frequent node.
					AddRelations(document, graph, dictionaryMap, currentVertex, nParameter, nDistanceCount, nextSentence, sectionType, builderType);
				}
			}
			return;
		}
        protected override void AddGraphBuilderProperties()
        {
            base.AddGraphBuilderProperties();
            AddProperty(new GraphProperty("Schenker", true));
            AddProperty(new GraphProperty("BiDirectional", false));
            AddProperty(new GraphProperty("IsDictionary", true));
        }

	}
}